1 // Debugging multimap implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/multimap.h
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_MULTIMAP_H
31 #define _GLIBCXX_DEBUG_MULTIMAP_H 1
33 #include <debug/safe_sequence.h>
34 #include <debug/safe_iterator.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 /// Class std::multimap with safety/checking/debug instrumentation.
42 template<typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
43 typename _Allocator = std::allocator<std::pair<const _Key, _Tp> > >
45 : public _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator>,
46 public __gnu_debug::_Safe_sequence<multimap<_Key, _Tp,
47 _Compare, _Allocator> >
49 typedef _GLIBCXX_STD_C::multimap<_Key, _Tp, _Compare, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_sequence<multimap> _Safe_base;
52 typedef typename _Base::const_iterator _Base_const_iterator;
53 typedef typename _Base::iterator _Base_iterator;
54 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
57 typedef _Key key_type;
58 typedef _Tp mapped_type;
59 typedef std::pair<const _Key, _Tp> value_type;
60 typedef _Compare key_compare;
61 typedef _Allocator allocator_type;
62 typedef typename _Base::reference reference;
63 typedef typename _Base::const_reference const_reference;
65 typedef __gnu_debug::_Safe_iterator<_Base_iterator, multimap>
67 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,
68 multimap> const_iterator;
70 typedef typename _Base::size_type size_type;
71 typedef typename _Base::difference_type difference_type;
72 typedef typename _Base::pointer pointer;
73 typedef typename _Base::const_pointer const_pointer;
74 typedef std::reverse_iterator<iterator> reverse_iterator;
75 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77 // 23.3.1.1 construct/copy/destroy:
78 explicit multimap(const _Compare& __comp = _Compare(),
79 const _Allocator& __a = _Allocator())
80 : _Base(__comp, __a) { }
82 template<typename _InputIterator>
83 multimap(_InputIterator __first, _InputIterator __last,
84 const _Compare& __comp = _Compare(),
85 const _Allocator& __a = _Allocator())
86 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
88 __gnu_debug::__base(__last),
91 multimap(const multimap& __x)
92 : _Base(__x), _Safe_base() { }
94 multimap(const _Base& __x)
95 : _Base(__x), _Safe_base() { }
97 #ifdef __GXX_EXPERIMENTAL_CXX0X__
98 multimap(multimap&& __x)
99 : _Base(std::move(__x)), _Safe_base()
100 { this->_M_swap(__x); }
102 multimap(initializer_list<value_type> __l,
103 const _Compare& __c = _Compare(),
104 const allocator_type& __a = allocator_type())
105 : _Base(__l, __c, __a), _Safe_base() { }
111 operator=(const multimap& __x)
113 *static_cast<_Base*>(this) = __x;
114 this->_M_invalidate_all();
118 #ifdef __GXX_EXPERIMENTAL_CXX0X__
120 operator=(multimap&& __x)
130 operator=(initializer_list<value_type> __l)
138 using _Base::get_allocator;
143 { return iterator(_Base::begin(), this); }
147 { return const_iterator(_Base::begin(), this); }
151 { return iterator(_Base::end(), this); }
155 { return const_iterator(_Base::end(), this); }
159 { return reverse_iterator(end()); }
161 const_reverse_iterator
163 { return const_reverse_iterator(end()); }
167 { return reverse_iterator(begin()); }
169 const_reverse_iterator
171 { return const_reverse_iterator(begin()); }
173 #ifdef __GXX_EXPERIMENTAL_CXX0X__
176 { return const_iterator(_Base::begin(), this); }
180 { return const_iterator(_Base::end(), this); }
182 const_reverse_iterator
184 { return const_reverse_iterator(end()); }
186 const_reverse_iterator
188 { return const_reverse_iterator(begin()); }
194 using _Base::max_size;
198 insert(const value_type& __x)
199 { return iterator(_Base::insert(__x), this); }
201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
202 template<typename _Pair, typename = typename
203 std::enable_if<std::is_convertible<_Pair,
204 value_type>::value>::type>
207 { return iterator(_Base::insert(std::forward<_Pair>(__x)), this); }
210 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 insert(std::initializer_list<value_type> __list)
213 { _Base::insert(__list); }
217 #ifdef __GXX_EXPERIMENTAL_CXX0X__
218 insert(const_iterator __position, const value_type& __x)
220 insert(iterator __position, const value_type& __x)
223 __glibcxx_check_insert(__position);
224 return iterator(_Base::insert(__position.base(), __x), this);
227 #ifdef __GXX_EXPERIMENTAL_CXX0X__
228 template<typename _Pair, typename = typename
229 std::enable_if<std::is_convertible<_Pair,
230 value_type>::value>::type>
232 insert(const_iterator __position, _Pair&& __x)
234 __glibcxx_check_insert(__position);
235 return iterator(_Base::insert(__position.base(),
236 std::forward<_Pair>(__x)), this);
240 template<typename _InputIterator>
242 insert(_InputIterator __first, _InputIterator __last)
244 __glibcxx_check_valid_range(__first, __last);
245 _Base::insert(__gnu_debug::__base(__first),
246 __gnu_debug::__base(__last));
249 #ifdef __GXX_EXPERIMENTAL_CXX0X__
251 erase(const_iterator __position)
253 __glibcxx_check_erase(__position);
254 this->_M_invalidate_if(_Equal(__position.base()));
255 return iterator(_Base::erase(__position.base()), this);
259 erase(iterator __position)
260 { return erase(const_iterator(__position)); }
263 erase(iterator __position)
265 __glibcxx_check_erase(__position);
266 this->_M_invalidate_if(_Equal(__position.base()));
267 _Base::erase(__position.base());
272 erase(const key_type& __x)
274 std::pair<_Base_iterator, _Base_iterator> __victims =
275 _Base::equal_range(__x);
276 size_type __count = 0;
277 _Base_iterator __victim = __victims.first;
278 while (__victim != __victims.second)
280 this->_M_invalidate_if(_Equal(__victim));
281 _Base::erase(__victim++);
287 #ifdef __GXX_EXPERIMENTAL_CXX0X__
289 erase(const_iterator __first, const_iterator __last)
291 // _GLIBCXX_RESOLVE_LIB_DEFECTS
292 // 151. can't currently clear() empty container
293 __glibcxx_check_erase_range(__first, __last);
294 for (_Base_const_iterator __victim = __first.base();
295 __victim != __last.base(); ++__victim)
297 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
298 _M_message(__gnu_debug::__msg_valid_range)
299 ._M_iterator(__first, "first")
300 ._M_iterator(__last, "last"));
301 this->_M_invalidate_if(_Equal(__victim));
303 return iterator(_Base::erase(__first.base(), __last.base()), this);
307 erase(iterator __first, iterator __last)
309 // _GLIBCXX_RESOLVE_LIB_DEFECTS
310 // 151. can't currently clear() empty container
311 __glibcxx_check_erase_range(__first, __last);
312 for (_Base_iterator __victim = __first.base();
313 __victim != __last.base(); ++__victim)
315 _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
316 _M_message(__gnu_debug::__msg_valid_range)
317 ._M_iterator(__first, "first")
318 ._M_iterator(__last, "last"));
319 this->_M_invalidate_if(_Equal(__victim));
321 _Base::erase(__first.base(), __last.base());
335 this->_M_invalidate_all();
340 using _Base::key_comp;
341 using _Base::value_comp;
343 // 23.3.1.3 multimap operations:
345 find(const key_type& __x)
346 { return iterator(_Base::find(__x), this); }
349 find(const key_type& __x) const
350 { return const_iterator(_Base::find(__x), this); }
355 lower_bound(const key_type& __x)
356 { return iterator(_Base::lower_bound(__x), this); }
359 lower_bound(const key_type& __x) const
360 { return const_iterator(_Base::lower_bound(__x), this); }
363 upper_bound(const key_type& __x)
364 { return iterator(_Base::upper_bound(__x), this); }
367 upper_bound(const key_type& __x) const
368 { return const_iterator(_Base::upper_bound(__x), this); }
370 std::pair<iterator,iterator>
371 equal_range(const key_type& __x)
373 std::pair<_Base_iterator, _Base_iterator> __res =
374 _Base::equal_range(__x);
375 return std::make_pair(iterator(__res.first, this),
376 iterator(__res.second, this));
379 std::pair<const_iterator,const_iterator>
380 equal_range(const key_type& __x) const
382 std::pair<_Base_const_iterator, _Base_const_iterator> __res =
383 _Base::equal_range(__x);
384 return std::make_pair(const_iterator(__res.first, this),
385 const_iterator(__res.second, this));
389 _M_base() { return *this; }
392 _M_base() const { return *this; }
398 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
399 this->_M_invalidate_if(_Not_equal(_Base::end()));
403 template<typename _Key, typename _Tp,
404 typename _Compare, typename _Allocator>
406 operator==(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
407 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
408 { return __lhs._M_base() == __rhs._M_base(); }
410 template<typename _Key, typename _Tp,
411 typename _Compare, typename _Allocator>
413 operator!=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
414 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
415 { return __lhs._M_base() != __rhs._M_base(); }
417 template<typename _Key, typename _Tp,
418 typename _Compare, typename _Allocator>
420 operator<(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
421 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
422 { return __lhs._M_base() < __rhs._M_base(); }
424 template<typename _Key, typename _Tp,
425 typename _Compare, typename _Allocator>
427 operator<=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
428 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
429 { return __lhs._M_base() <= __rhs._M_base(); }
431 template<typename _Key, typename _Tp,
432 typename _Compare, typename _Allocator>
434 operator>=(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
435 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
436 { return __lhs._M_base() >= __rhs._M_base(); }
438 template<typename _Key, typename _Tp,
439 typename _Compare, typename _Allocator>
441 operator>(const multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
442 const multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
443 { return __lhs._M_base() > __rhs._M_base(); }
445 template<typename _Key, typename _Tp,
446 typename _Compare, typename _Allocator>
448 swap(multimap<_Key, _Tp, _Compare, _Allocator>& __lhs,
449 multimap<_Key, _Tp, _Compare, _Allocator>& __rhs)
450 { __lhs.swap(__rhs); }
452 } // namespace __debug